package com.cxps.cloud.controller;

import com.cxps.cloud.common.Apiresponse;
import com.cxps.cloud.domain.User;
import com.cxps.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 郝振坤
 * @Date 2022/1/6 11:41
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 这是新增的接口
     * url：http://172.20.10.9:2001/user/user/addUser?name
     * 必传参数:(string)name
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Apiresponse addUser(User user){
        userService.addUser(user);
        return Apiresponse.succee("");
    }

    /**
     * 这是user修改的接口
     * url:http://172.20.10.9:2001/user/user/updateUser?id
     * 必传参数：(integer)id
     * 返回值类型：{
     *     "status": 200,
     *     "info": "msg",
     *     "data": ""
     * }
     * */
    @PostMapping("/updateUser")
    public Apiresponse updateUser(User user){
        try {
            if (user.getId()==null){
                return  Apiresponse.error(400,"请添加修改的内容 注意！！密码在这不能修改");
            }
            userService.updateUser(user);
            return Apiresponse.succee("");
        }catch (Exception e){
            return  Apiresponse.error(500,e.getMessage());
        }
    }

    /**正常查询全部的数据
     * url:http://172.20.10.9:2001/user/user/queryAllDataUser?name
     * 必传值：(string name)
     *返回值类型：{
     *"status": 200,
     *"info": "msg",
     *"data":
     */
    @RequestMapping("queryAllDataUser")
    public Apiresponse queryAllDataUser(User user) {
        try {
            if (user.getName() == null) {
                return Apiresponse.error(400, "参数错误");
            }

            List<User> users = userService.queryAllDataUser(user);
            return Apiresponse.succee(users);
        }catch (Exception e){
            return Apiresponse.error(500,e.getMessage());
        }
    }

    /**
     * 这是删除的接口
     * url：http://172.20.10.9:2001/user/user/deleteUser?id=11
     * 必传参数：(integer)id
     返回值类型：{
     *     "status": 200,
     *     "info": "msg",
     *     "data": ""
     * }
     */
    @GetMapping("/deleteUser")
    public Apiresponse deleteUser(Integer id){
        try {
            if (id==null){
                return Apiresponse.error(400,"参数错误");
            }
            userService.deleteUser(id);
            return Apiresponse.succee("");
        }catch (Exception e){
            return Apiresponse.error(500,e.getMessage());
        }
    }

}
